﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 09/03/2010
 * Time: 9:11
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.general.views
{
	/// <summary>
	/// Description of frmWarehouseTransference.
	/// </summary>
	public partial class frmWarehouseTransference : KryptonForm
	{
		Warehouses listWarehouses = new Warehouses();
		Stocks listStocks = new Stocks();
		common.Search search = new csharp.common.Search();
		
		
		public frmWarehouseTransference()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		private void fillComboWarehouse()
		{
			int tam = listWarehouses.count();
			
			cbxWarehouse.Items.Clear();
			cbxWarehouseTarget.Items.Clear();
			cbxWarehouse.Text 		= " ";
			cbxWarehouseTarget.Text = " ";
			
			for(int i = 0; i < tam; i++)
			{
				cbxWarehouse.Items.Add(listWarehouses.get(i));
				cbxWarehouseTarget.Items.Add(listWarehouses.get(i));
			}
			cbxWarehouse.ValueMember   			= "id";
			cbxWarehouseTarget.ValueMember 		= "id";
			cbxWarehouse.DisplayMember 			= "name";
			cbxWarehouseTarget.DisplayMember 	= "name";
		}
		
		void FrmWarehouseTransferenceLoad(object sender, EventArgs e)
		{
			listWarehouses.fillList();
			fillComboWarehouse();
		}
		
		void CbxWarehouseMouseLeave(object sender, EventArgs e)
		{
			if(cbxWarehouse.SelectedIndex >= 0)
			{
				fillDataGridView();
			}
		}
		
		protected void fillDataGridView()
		{			
			if(!fillListByWarehouse(((Warehouse)cbxWarehouse.SelectedItem).id))
			{
				MessageBox.Show("Erro.:"+search.getMessageError());
				return;
			}
			
			dgvMain.Rows.Clear();
			System.Data.DataSet rs = search.getResultSet();
			int tam = rs.Tables[0].Rows.Count;
			for(int i = 0; i < tam; i++)
			{
				dgvMain.Rows.Add(new String[] { rs.Tables[0].Rows[i]["product_id"].ToString(),
				                 				rs.Tables[0].Rows[i]["product_code"].ToString(),
				                 				rs.Tables[0].Rows[i]["product_name"].ToString(),
				                 				rs.Tables[0].Rows[i]["stock_atual_value"].ToString()});
			}
		}
		
		private Boolean fillListByWarehouse(string idValue)
		{
			search.prepare();
			search.setFields("*");
			search.setTable("common.stock");
			search.setTable("common.product", "stock_product_id = product_id");
						
			search.setConditionalWhere("stock_warehouse_id = "+idValue);
			return search.execute();
		}
		
		void BtnExitClick(object sender, EventArgs e)
		{
			this.Close();
			this.Dispose();
		}
		
		void BtnTransferenceClick(object sender, EventArgs e)
		{
			int index = dgvMain.SelectedRows.Count;
			if (index <= 0)
			{
				MessageBox.Show("Por favor selecione um produto!");			
				return;
			}
			if(!common.Util.validateForm(this))
			{
				return;
			}
			int quant = Convert.ToInt32(txtQuant.Text);
			int quantProd =  Convert.ToInt32(dgvMain.CurrentRow.Cells["product_quant"].Value.ToString());
			if( quant > quantProd)
			{
				MessageBox.Show("Erro, quantidade a ser tranferida utrapassa a quantidade estocada");
				return;
			}
			//Registra entrada na tabela stocklog
			general.StockLog stocklog = new csharp.rad.general.StockLog();
			stocklog.warehouse_id 	= (cbxWarehouseTarget.SelectedItem as general.Warehouse).id;
			stocklog.product_id 	= dgvMain.CurrentRow.Cells["product_id"].Value.ToString();
			stocklog.quant      	= txtQuant.Text;
			stocklog.date_mov	   	= DateTime.Now.ToString("dd/MM/yyyy");
			stocklog.type_mov		= "E";
			stocklog.user_id  		= Information.userId;
			stocklog.historic 		= "Tranferencia de produtos do almoxarifado "+(cbxWarehouse.SelectedItem as Warehouse).name+" para "+(cbxWarehouseTarget.SelectedItem as Warehouse).name;
			if(!stocklog.save())
			{
				MessageBox.Show("Erro ao tentar gravar o registro"+ stocklog.errorMessage);
				common.Database.RollBack();
				return;
			}
			
			//Registra saida na tabela stocklog
			stocklog.clear();
			stocklog.warehouse_id 	= (cbxWarehouse.SelectedItem as Warehouse).id;
			stocklog.product_id 	= dgvMain.CurrentRow.Cells["product_id"].Value.ToString();
			stocklog.quant      	= txtQuant.Text;
			stocklog.date_mov	   	= DateTime.Now.ToString("dd/MM/yyyy");
			stocklog.type_mov		= "S";
			stocklog.user_id  		= Information.userId;
			stocklog.historic 		= "Tranferencia de produtos do almoxarifado "+(cbxWarehouse.SelectedItem as Warehouse).name+" para "+(cbxWarehouseTarget.SelectedItem as Warehouse).name;
			if(!stocklog.save())
			{
				MessageBox.Show("Erro ao tentar gravar o registro"+ stocklog.errorMessage);
				common.Database.RollBack();
				return;
			}
			
			MessageBox.Show("Its transference can not be completed");
		}
	}
}
